package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.MinioUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        //   获取文件名
        String originalFilename = file.getOriginalFilename();
        //   保证文件名唯一
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        //   存储到本地磁盘
//        file.transferTo(new File("E:\\Code\\codeData\\big-event" + filename));
        //   存储到minio
        String url = MinioUtil.uploadFile(filename, file);
        if(url == null){
            return Result.error("上传失败");
        }
        return Result.success(url);
    }
}
